xquery version "1.0-ml";
(:declare namespace http-util="http://marklogic.com/util/http";:)
(:import module namespace functx = "http://www.functx.com" at "/MarkLogic/functx/functx-1.0-nodoc-2007-01.xqy";:)
import module namespace http-util = "http://marklogic.com/util/http" at "http.xqy";
declare default function namespace "http://www.w3.org/2005/xpath-functions";
declare namespace http="xdmp:http";

declare private function http-util:is-atom() as xs:boolean {

};

declare private function http-util:is-html() as xs:boolean {

};

declare private function http-util:is-pox() as xs:boolean {

};


let $available-variants as element(http:variant)+ := (
	http-util:string-to-variant("text/html"),
	http-util:string-to-variant("application/atom+xml"),
	http-util:string-to-variant("application/xml"),
	(: What if the browser actually wants non-text/html? :)
	http-util:string-to-variant("image/png")
)
let $user-agent as xs:string? := xdmp:get-request-header("User-Agent")
let $accept as xs:string? := http-util:what-browsers-really-want($user-agent, xdmp:get-request-header("Accept")) (: What about missing or “broken” Accept headers? What about URL-based content types (e.g. XXXX.xml or XXXX.atom) What about the difference between text/html and application/xhtml+xml? :)
let $variants as element(http:variants) := http-util:parse-accept-header($accept)
let $preferred-variant as element(http:variant)? := http-util:preferred-variant($variants, $available-variants)


return 
	if(empty($preferred-variant)) then
		error(xs:QName("http:error"), "405", "Not Acceptable")
	else
	(
		xdmp:set-response-content-type("text/plaing"), 
		string-join((
			concat("Available Variants: ", string-join(http-util:variant-to-string((: Watch out: function mapping! :)$variants/http:variant), ", ")),
			concat("User Agent: ", $user-agent),
			concat("Accept: ", xdmp:get-request-header("Accept")),
			concat("Calculated Accept: ", $accept),
			concat("Preferred Variant: ", http-util:variant-to-string($preferred-variant))
		), "
===============================
")
	)